package com.pg.interceptor;

import com.pg.utils.SessionUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if (token==null){
            //token为空
            response.setStatus(401);
            return false; 
        }

        Object o = SessionUtils.get(token);
        if (o==null){
            //token不正确
            response.setStatus(401);
            return false;
        }

        String[] split = token.split("-");
        String time=split[1];

        if (time.equals("")){
            //token为空
            response.setStatus(401);
            return false;
        }

        if (System.currentTimeMillis()-Long.parseLong(time)>3600*2*1000){
            //token过期
            response.setStatus(403);
            return false;
        }
        return true;
    }
}
